package cm.enums;

import cm.imf.DataMaskingOperation;
import org.springframework.util.StringUtils;

/**
 * @author: yxh
 * @Description: 注解脱敏枚举
 * @Date: Created in 2022/8/29 10:51
 * @Modified By:
 */
public enum DataMaskingFunc {

    /**
     * 脱敏转换器
     */
    NO_MASK((str, maskChar) -> {
        return str;
    }),
    ALL_MASK((str, maskChar) -> {
        if (StringUtils.hasLength(str)) {
            StringBuilder sb = new StringBuilder();
            sb.append(str.substring(0, 1));
            int dataMask = str.length();
            if (str.length() > 2) {
                dataMask = str.length() - 1;
            }
            for (int i = 1; i < dataMask; i++) {
                sb.append(StringUtils.hasLength(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR);
            }
            if (str.length() > 2) {
                sb.append(str.substring(str.length() - 1, str.length()));
            }

            return sb.toString();
        } else {
            return str;
        }
    });

    private final DataMaskingOperation operation;

    private DataMaskingFunc(DataMaskingOperation operation) {
        this.operation = operation;
    }


    public DataMaskingOperation operation() {
        return this.operation;
    }
}

